package Test;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

public class Test01 extends Thread {
    //服务器上存有多个用户对象,考虑用List或者Map（对象包括用户和密码），客户端通过socket登录，如果成功，输出欢迎信息，失败给出提示。
    private final Socket s;
    public Test01(Socket s){
        this.s=s;
    }

    private static Map<String, String> users = new HashMap<>();

    static {
        users.put("user", "password");
        users.put("admin", "admin");
    }

    @Override
    public void run() {
        System.out.println("socket连接建立了。。。" + s);
        try {
            InputStream in = s.getInputStream();
            OutputStream out = s.getOutputStream();
            //客户端回，服务器答，
            //BufferedWriter pw=new BufferedWriter(out,true);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            PrintWriter pw = new PrintWriter(out, true);
            String name = br.readLine();
            String pass = br.readLine();
            if (pass.equals(users.get(name))) {
                pw.println("登录成功，欢迎进入主页！");
            } else {
                pw.println("登录失败,账户或密码错误！");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                s.close();
            } catch (Exception e) {
            }
        }
    }
}
